/*
 * Copyright 2011.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.queryhandler.n.client.ui.actions.db;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.sql.SQLWarning;
import org.opu.queryhandler.n.client.services.DataRepresentationService;
import org.opu.queryhandler.n.client.services.DatabaseService;
import org.opu.queryhandler.n.db.QueryResult;
import org.opu.queryhandler.n.util.TextResource;

/**
 *
 */
public class ExecuteQueryAction extends DbAction {

    public static String MNAME = "ExecuteQuery";
    private TextResource textResource;
    private DataRepresentationService representationService;

    public ExecuteQueryAction(DatabaseService dbService,
            DataRepresentationService representationService) {
        super(dbService);
        this.representationService = representationService;
    }

    @Override
    public String getName() {
        return MNAME;
    }

    @Override
    public void action(ActionEvent e) {
        if (dbService.isDatabaseAvilable()) {
            String query = textResource.getText();
            String[] headers = null;
            Object data;
            SQLWarning warning;
            QueryResult sendQuery;

            try{
                sendQuery = dbService.executeQuery(query);
                headers = sendQuery.getHeads();
                int r = sendQuery.getRows();
                int c = sendQuery.getColumns();
                if (r <= 0 || c <= 0) {
                    data = new String[1][headers.length];
                } else {
                    String arr[][] = new String[r][c];
                    for (int i = 0; i < r; i++) {
                        for (int j = 0; j < c; j++) {
                            arr[i][j] = representationService.getString(sendQuery, i, j);
                        }
                    }
                    data = arr;
                }
                warning = sendQuery.warnings;

            } catch(SQLException ex) {
                data = boundle.getString(MNAME + ".Title.NoRes");
                warning = new SQLWarning((String)data, ex);
                while (ex != null) {
                    warning.setNextWarning(new SQLWarning(ex.getLocalizedMessage(), ex));
                    ex = ex.getNextException();
                }
            }

            textResource.setResult(query, headers, data, warning);
        } else {
            errorNoConnect();
        }
    }

    public TextResource getTextResource() {
        return textResource;
    }

    public void setTextResource(TextResource textResource) {
        this.textResource = textResource;
    }
}
